﻿using Kingdee.BOS.Orm.DataEntity;
using SD.WRZS.K3.ServiceFacade.KDServiceEntity.Mobile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace SDWXServer.Models
{
    /// <summary>
    /// 采购入库单
    /// </summary>
    [Serializable]
    public class WXSTKInStock : AbstractBillObject
    {
        public WXSTKInStock()
        {
            Organization = new Organization();
            Supplier = new Supplier();
            EntryRows = new List<WXSTKInStockEntryRow>();
        }
        /// <summary>
        /// Id
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// AppId
        /// </summary>
        public string AppId { get; set; }
        /// <summary>
        /// 内码
        /// </summary>
        public Int64 BillId { get; set; }
        /// <summary>
        /// 单据编码
        /// </summary>
        public string BillNO { get; set; }
        /// <summary>
        /// 单据状态
        /// </summary>
        public string DocumentStatus { get; set; }
        /// <summary>
        /// 单据状态名称
        /// </summary>
        public string DocumentStatusName { get; set; }
        /// <summary>
        /// 业务日期
        /// </summary>
        public DateTime BillDate { get; set; }
        /// <summary>
        /// 组织
        /// </summary>
        public Organization Organization { get; set; }
        /// <summary>
        /// 乙方
        /// </summary>
        public Supplier Supplier { get; set; }
        /// <summary>
        /// 工程项目Id
        /// </summary>
        public Int64 GCXMId { get; set; }
        /// <summary>
        /// 工程项目编码
        /// </summary>
        public string GCXMNumber { get; set; }
        /// <summary>
        /// 工程项目名称
        /// </summary>
        public string GCXMName { get; set; }
        public List<WXSTKInStockEntryRow> EntryRows { get; set; }
        public class WXSTKInStockEntryRow
        {
            /// <summary>
            ///行内码
            /// </summary>
            public Int64 EntryId { get; set; }
            /// <summary>
            /// 物料内码
            /// </summary>
            public Int64 ItemId { get; set; }
            /// <summary>
            /// 物料编码
            /// </summary>
            public string ItemNumber { get; set; }
            /// <summary>
            /// 物料名称
            /// </summary>
            public string ItemName { get; set; }
            /// <summary>
            /// 单位内码
            /// </summary>
            public Int64 UnitId { get; set; }
            /// <summary>
            /// 单位编码
            /// </summary>
            public string UnitNumber { get; set; }
            /// <summary>
            /// 单位名称
            /// </summary>
            public string UnitName { get; set; }
            /// <summary>
            /// 预算物料规格
            /// </summary>
            public string ItemNote { get; set; }
            /// <summary>
            /// 实收数量
            /// </summary>
            public decimal RealQty { get; set; }
            /// <summary>
            /// 项目仓库Id
            /// </summary>
            public Int64 StockId { get; set; }
            /// <summary>
            /// 项目仓库编码
            /// </summary>
            public string StockNumber { get; set; }
            /// <summary>
            /// 项目仓库名称
            /// </summary>
            public string StockName { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string Note { get; set; }
            /// <summary>
            /// show
            /// </summary>
            public bool Show { get; set; } = false;
        }
        public static WXSTKInStock Parse(DynamicObject obj)
        {
            var item = new WXSTKInStock();
            item.Id = string.Format("SZGD_STKInStock_{0}", Convert.ToInt64(obj["Id"]));
            item.AppId = string.Format("SZGD_STKInStock_{0}", Convert.ToInt64(obj["Id"]));
            item.BillId = Convert.ToInt64(obj["Id"]);
            item.BillNO = Convert.ToString(obj["BillNo"]);
            item.BillDate = Convert.ToDateTime(obj["Date"]);
            item.DocumentStatus = Convert.ToString(obj["DocumentStatus"]);
            switch (item.DocumentStatus)
            {
                case "Z":
                    item.DocumentStatusName = "暂存";
                    break;
                case "A":
                    item.DocumentStatusName = "创建";
                    break;
                case "B":
                    item.DocumentStatusName = "审核中";
                    break;
                case "C":
                    item.DocumentStatusName = "已审核";
                    break;
                case "D":
                    item.DocumentStatusName = "重新审核";
                    break;
            }
            var orgObj = obj["PurchaseOrgId"] as DynamicObject;
            item.Organization.Id = Convert.ToInt64(orgObj["Id"]);
            item.Organization.Number = Convert.ToString(orgObj["Number"]);
            item.Organization.Name = Convert.ToString(orgObj["Name"]);
            var gcxmObj = obj["FGCXMID"] as DynamicObject;
            if (gcxmObj != null)
            {
                item.GCXMId = Convert.ToInt64(gcxmObj["Id"]);
                item.GCXMNumber = Convert.ToString(gcxmObj["Number"]);
                item.GCXMName = Convert.ToString(gcxmObj["Name"]);
            }
            var supObj = obj["SupplierId"] as DynamicObject;
            if (supObj != null)
            {
                item.Supplier.Id = Convert.ToInt64(supObj["Id"]);
                item.Supplier.Number = Convert.ToString(supObj["Number"]);
                item.Supplier.Name = Convert.ToString(supObj["Name"]);
            }
            var entryRows = obj["InStockEntry"] as DynamicObjectCollection;
            foreach (var entryRow in entryRows)
            {
                var rows = new WXSTKInStockEntryRow();
                rows.EntryId = Convert.ToInt64(entryRow["Id"]);
                var materialobj = entryRow["MaterialId"] as DynamicObject;
                if (materialobj != null)
                {
                    rows.ItemId = Convert.ToInt64(materialobj["Id"]);
                    rows.ItemNumber = Convert.ToString(materialobj["Number"]);
                    rows.ItemName = Convert.ToString(materialobj["Name"]);
                }
                var unitObj = entryRow["UnitID"] as DynamicObject;
                if (unitObj != null)
                {
                    rows.UnitId = Convert.ToInt64(unitObj["Id"]);
                    rows.UnitNumber = Convert.ToString(unitObj["Number"]);
                    rows.UnitName = Convert.ToString(unitObj["Name"]);
                }
                var stockobj = entryRow["StockId"] as DynamicObject;
                if (stockobj != null)
                {
                    rows.StockId = Convert.ToInt64(stockobj["Id"]);
                    rows.StockNumber = Convert.ToString(stockobj["Number"]);
                    rows.StockName = Convert.ToString(stockobj["Name"]);
                }
                rows.RealQty = Convert.ToDecimal(entryRow["RealQty"]);
                rows.Note = Convert.ToString(entryRow["Note"]);
                item.EntryRows.Add(rows);
            }
            return item;
        }

    }
}